package cn.wolfcode.web.controller;

import cn.wolfcode.ssm.domain.Department;
import cn.wolfcode.ssm.page.JsonPage;
import cn.wolfcode.ssm.page.PageResult;
import cn.wolfcode.ssm.query.QueryObject;
import cn.wolfcode.ssm.service.IDepartmentService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")

public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    //@RequiresPermissions(value = {"role:list","部门列表"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") QueryObject qo){
       // System.out.println(SecurityUtils.getSubject().hasRole("HR_MGR"));
       // System.out.println(SecurityUtils.getSubject().isPermitted("customer:potentialList"));
        // PageResult<Department> pageResult = departmentService.query(qo);
        model.addAttribute("pageResult",departmentService.query(qo));
        return "/department/list";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JsonPage delete(Long id){
        try {
            if (id != null){
                departmentService.delete(id);
            }
            return new JsonPage();
        }catch (Exception e){
            e.printStackTrace();
            return new JsonPage(false,"删除失败");
        }
       // return new JsonPage();
    }

 /*   @RequestMapping("/input")
    public String input(Model model, Long id){
        if (id != null){
            Department department = departmentService.get(id);
            model.addAttribute("department",department);
        }
        return "/department/input";
    }*/

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonPage saveOrUpdate(Department department){
        try {
            if (department.getId() != null){
                departmentService.update(department);
            }else{
                departmentService.save(department);
            }
            return new JsonPage();
        }catch (Exception e){
            e.printStackTrace();
            return new JsonPage(false,"保存失败");
        }

    }
}
